package Scuttlebutt;

import java.awt.event.*;
import javax.swing.*;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import java.io.*;
import java.lang.Enum;

enum type {file, folder, link} ;
	
public class StringInputDialog extends JDialog 
{
	String label = null;
	String text = null;
	JTextField link = null;
	CancelButtonAction cancelAction = null;
	boolean bState = false;
	OKButtonAction OKAction = null;
	JButton OKbutton = null;
	JButton cancel = null;
	String path = null;
	type nameType = null;
	
	public class OKButtonAction extends AbstractAction {
        public OKButtonAction(String text, Integer mnemonic) {
            super(text, null);
            putValue(MNEMONIC_KEY, mnemonic);
        }
        
        public void actionPerformed(ActionEvent e) {
			switch(nameType){
				case file:{
			        String name = link.getText();
			        String file = new String(path + File.separatorChar + name);
					String ext = getExtension(new File(file));
					if(ext == null){
						name += new String(".html");
//						System.out.println("name:" + name);
					}
			        if (link.getText().length() == 0){
				        JOptionPane.showMessageDialog(null, label + " cannot be blank", "Error", JOptionPane.ERROR_MESSAGE);
				        link.grabFocus();
				        
				        return;
			        }
					
					File f = new File(path + File.separatorChar + name);
//					System.out.println("path:" + f.getPath());
					if(f.exists()){
				        JOptionPane.showMessageDialog(null, new String("A file with the name \"" + link.getText() + "\" already exists"), "Error", JOptionPane.ERROR_MESSAGE);
						link.grabFocus();
					}else{
						setVisible(false);
			        	bState = true;
			        	link.setText(name);
					}
				}
				break;
				case folder:{
			        if (link.getText().length() == 0){
				        JOptionPane.showMessageDialog(null, label + " cannot be blank", "Error", JOptionPane.ERROR_MESSAGE);
				        link.grabFocus();
				        return;
			        }
			        
					File f = new File(path + File.separatorChar + link.getText());
//					System.out.println("path:" + f.getPath());
					if(f.exists()){
				        JOptionPane.showMessageDialog(null, new String("A folder with the name \"" + link.getText() + "\" already exists"), "Error", JOptionPane.ERROR_MESSAGE);
						link.grabFocus();
					}else{
						setVisible(false);
			        	bState = true;
					}
			        
				}
				break;
				case link:{
			        if (link.getText().length() == 0){
				        JOptionPane.showMessageDialog(null, label + " cannot be blank", "Error", JOptionPane.ERROR_MESSAGE);
				        link.grabFocus();
				       
				        return;
			        }
			        
					File f = new File(path + File.separatorChar + link.getText());
//					System.out.println("path:" + f.getPath());
					if(f.exists()){
				        JOptionPane.showMessageDialog(null, new String("A folder with the name \"" + link.getText() + "\" already exists"), "Error", JOptionPane.ERROR_MESSAGE);
						link.grabFocus();
					}else{
						setVisible(false);
			        	bState = true;
					}
				}
				break;
	        }
        }
        
	    public String getExtension(File nf) {
	        String ext = null;
	        String s = nf.getName();
	        int i = s.lastIndexOf('.');
	
	        if (i > 0 &&  i < s.length() - 1) {
	            ext = s.substring(i+1).toLowerCase();
	        }
	        return ext;
	    }
        
    }	
	
	
	public class CancelButtonAction extends AbstractAction {
        public CancelButtonAction(String text, Integer mnemonic) {
            super(text, null);
            putValue(MNEMONIC_KEY, mnemonic);
        }
        public void actionPerformed(ActionEvent e) {
	        setVisible(false);
	        bState = false;
        }
    }	
	
	public StringInputDialog(type nameType, Scuttlebutt parent, String title, String label, String text, String path)
	{
		super(parent, title, true);
		this.label = label;
		this.path = path;
		this.nameType = nameType;
//		this.text = checkFolderName(text);
		this.text = text.trim();
//		System.out.println("text a:" + text);
		initializeMethod();
		
		setLocationRelativeTo(parent);
	}
	
	public StringInputDialog(type nameType, Scuttlebutt parent, String title, String label, String text)
	{
		super(parent, title, true);
		this.label = label;
		this.nameType = nameType;
//		this.text = checkFolderName(text);
		this.text = text.trim();
//		System.out.println("text b:" + text);
		initializeMethod();
		setLocationRelativeTo(parent);
	}

	private String checkFolderName(String text){
		Integer i = new Integer(0);
		File f = new File(path + File.separatorChar + text);
//		System.out.println("file:" + f.getPath());
		while(f.exists()){
			
			int idx = f.getPath().lastIndexOf('.');
			if(idx != -1){
				StringBuffer p = new StringBuffer(f.getPath());
				p.insert(idx, i.toString());
				f = new File(p.toString());
			}else{
				f = new File(path + File.separatorChar + text);
			}
			i++;
//			System.out.println("file:" + f.getPath());
		}

		return f.getName();
	}
	
	public String getValue(){
		return link.getText();
	}

	private void initializeMethod()
	{
		Container container=getContentPane();
		container.setLayout(null);
		setupDialog(container);
	}
	
	public boolean getState(){
		return bState;
	}
	
	public void setupDialog(Container pane){
		OKAction = new OKButtonAction("OK",new Integer(KeyEvent.VK_O));
		cancelAction = new CancelButtonAction("Cancel",new Integer(KeyEvent.VK_C));
		Insets insets = pane.getInsets();
		int headspace = 5;
		int between = 5;
		int left_margin = 20;
		
		JLabel name = new JLabel(label);
		pane.add(name);
		Dimension size = name.getPreferredSize();
		name.setBounds(insets.left + left_margin, insets.top + headspace, size.width, size.height);

		int x = insets.left + left_margin + name.getSize().width;
		int y = insets.top + headspace;
//		System.out.println("y:"+ y);
		link = new JTextField(15);
		link.setText(text.trim());
		pane.add(link);
		size = link.getPreferredSize();
		link.setBounds(x + between, y, size.width, size.height);
		y += link.getPreferredSize().height;
//		System.out.println("y:"+ y);
		
		if(nameType == type.link){
			JLabel url = new JLabel("URL:");
			pane.add(url);
			size = url.getPreferredSize();
			url.setBounds(insets.left + left_margin,y, size.width, size.height);
			
			JLabel location = new JLabel(text);
			pane.add(location);
			size = location.getPreferredSize();
			location.setBounds(insets.left + left_margin + url.getPreferredSize().width + between,y, size.width, size.height);
			
			
			y += size.height + headspace;
//			System.out.println("y:"+ y);

		}
		
		cancel = new JButton(cancelAction);
		pane.add(cancel);
		size = cancel.getPreferredSize();
		cancel.setBounds(x + between + link.getPreferredSize().width - size.width - 1, y, size.width, size.height);

		OKbutton = new JButton(OKAction);
		pane.add(OKbutton);
		size = OKbutton.getPreferredSize();
		OKbutton.setBounds(x + link.getPreferredSize().width - cancel.getPreferredSize().width - size.width - 1, y, size.width, size.height);
		
		y += size.getHeight() + headspace;
//		System.out.println("size.getHeight():" + size.getHeight());
//		System.out.println("y:"+ y);
		
		setSize(new Dimension(left_margin + left_margin + between + name.getPreferredSize().width + link.getPreferredSize().width, y + 30));
	
	}

}

